package net.lab1024.sa.gateway.processor;

import net.lab1024.sa.gateway.dto.ApiProcessDTO;
import net.lab1024.sa.gateway.dto.HttpRequestDTO;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Component
public class ExecuteProcessor extends AbstractApiProcessor {
    private final WebClient webClient;
    private final ProcessorFactory processorFactory;

    public ExecuteProcessor(WebClient.Builder webClientBuilder, ProcessorFactory processorFactory) {
        this.webClient = webClientBuilder.build();
        this.processorFactory = processorFactory;
    }

    @Override
    protected Mono<Void> doProcess(ApiProcessDTO dto) {
        HttpRequestDTO request = dto.getHttpRequest();
        // 根据协议类型（HTTP/Dubbo）调用后端服务
        return processorFactory.get(request.getProtocol())
                .process(dto)
                .doOnSuccess(result -> dto.setResult(result))
                .then();
    }

}